﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;

namespace YArchitech.Public.ExcelToView
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class Command : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
            Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
			Document document = application.ActiveUIDocument.Document;
			if (Command._hWndRevit == null)
			{
				Command._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			try
			{
				List<bool> list = new List<bool>();
				new SheetViewMakeManger(document);
				SelectDataForm selectDataForm = new SelectDataForm(commandData, Command._hWndRevit);
				if (selectDataForm.ShowDialog(Command._hWndRevit) == DialogResult.OK)
				{
					list = selectDataForm.Lstblresult;
					if (list.Count == 0)
					{
						ExcelLoader_LateBound.CloseLoader();
						return Autodesk.Revit.UI.Result.Succeeded;
					}
				}
				int count = list.FindAll((bool k) => k).Count;
				YJKMessageBox.Show(string.Format("生成{0}/{1}个视图", count, list.Count));
				ExcelLoader_LateBound.CloseLoader();
			}
			catch (ArgumentException ex)
			{
				if (ex.Message.Equals("没有安装Microsoft Office Excel。"))
				{
					YJKMessageBox.Show("没有安装Microsoft Office Excel, 请安装Microsoft Office Excel。");
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static WindowHandle _hWndRevit;
	}
}
